#include "Snake.h"
#include <vector>
#include "Position.h"
#include <string>
#include <sstream>
#include <iostream>
using namespace std;


void Snake::Receive(string str){
int size;
vector <int> r1;
vector <int> r2;
body.clear();

    if(str.substr(1, 1) == "%"){
size = strToInt(str.substr(0,1));
    }
    else if(str.substr(2, 1) == "%"){
        size = strToInt(str.substr(0,2));
            }
    else if(str.substr(3, 1) == "%"){
        size = strToInt(str.substr(0,3));
            }

    for(int i = 0; i < size; i++){

    }

    if(size == 0){
        //return null;
    }
    else{

        for(int i = 0; i < str.length(); i++){
            if(str[i] == '['){
            r1.push_back(i);
            }

            if(str[i] == ']'){
            r2.push_back(i);
            }
        }

    }
vector <string> fragmenty;
    for(int i = 0; i < r1.size(); i++){

        fragmenty.push_back(str.substr(r1[i]+1, r2[i]-r1[i]-1));
         //cout << fragmenty[i] << endl;
         int r;
         for(int j = 0; j < ((string)fragmenty[i]).length(); j++){
             if(((string)fragmenty[i])[j] == '-'){
                r = j;
             }

         }

         //cout << ((string)fragmenty[i]).substr(0, r) << " * " << ((string)fragmenty[i]).substr(r+1)  << endl;
         Position ps;
         int x = strToInt(((string)fragmenty[i]).substr(0, r));
         int y = strToInt(((string)fragmenty[i]).substr(r+1));
         ps.Set(x, y);
         body.push_back(ps);
    }
}


string Snake::Send(){
string str = "";
str = int2str(body.size()) + "%";

for(int i = 0; i < body.size(); i++){
    string tmp;
    Position ps = body[i];
    tmp = "[";
    int x = ps.GetX();
    string xs = int2str(x);
    tmp = tmp + xs;
    tmp = tmp + "-";
    int y = ps.GetY();
    string ys = int2str(y);
        tmp = tmp + ys;
    tmp = tmp +"]";
    str = str+tmp;
}

return str;
}

int Snake::strToInt(const string &str)
{
stringstream ss(str);
int n;
ss >> n;
return n;
}

string Snake::int2str(int n){
 stringstream ss;
 ss << n;
 return ss.str();
 }


void Snake::SetHead(Position ps){
        body.insert(body.begin(), ps);
}

int Snake::GetSize(){
        return body.size();
}

void Snake::RemoveTail(){
        body.pop_back();
}

void Snake::AddElement(Position ps){
        body.push_back(ps);
}

void Snake::Show(){
        if(body.size() > 0){
        for(int i = 0; i < body.size(); i++){
                body[i].Show();
        }
        }
}

Position Snake::GetHead(){
	return body[0];
}

bool Snake::MoveStep(int dir){
	Position tmp = Position();
switch(dir)
{
case 1: // gora
	//if(last_direction != dir){
	tmp = this->GetHead();
	tmp.Yminus();
	this->SetHead(tmp);
        //this->RemoveTail();
	last_direction = 1;
	//}
break;

case 2: // dol
	//if(last_direction != dir){
	tmp = this->GetHead();
	tmp.Yplus();
	this->SetHead(tmp);
        //this->RemoveTail();
	last_direction = 2;
	//}
break;

case 3: // lewo
	//if(last_direction != dir){
	tmp = this->GetHead();
	tmp.Xminus();
	this->SetHead(tmp);
        //this->RemoveTail();
	last_direction = 3;
//	}
break;

case 4: // lewo
	//if(last_direction != dir){
	tmp = this->GetHead();
	tmp.Xplus();
	this->SetHead(tmp);
        //this->RemoveTail();
	last_direction = 4;
	//}
break;

default:
break;
}
return true;
}

Snake::Snake(void)
{
	last_direction = 3;
}

Snake::~Snake(void)
{
}
